<template>
<div class="app-container">
  <el-row :gutter="10" class="mb8">
    <el-col :span="1.5">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
      >新增</el-button>
    </el-col>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  </el-row>

  <el-table v-loading="loading" :data="formList" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="表单编码" align="center" prop="code" width="100"/>
    <el-table-column label="表单名称" align="center" prop="formName" width="300"/>
    <el-table-column label="附加表" align="center" prop="tableName" width="200">
      <template slot-scope="scope">
        {{'system_'+scope.row.tableName}}
      </template>
    </el-table-column>
    <el-table-column label="状态" align="center" prop="type" width="100">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.cms_form_type" :value="scope.row.type"/>
      </template>
    </el-table-column>
    <el-table-column label="创建时间" align="center" prop="createTime" width="100">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
      </template>
    </el-table-column>
    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
      <template slot-scope="scope">
        <el-button
          size="mini"
          type="success"
          icon="el-icon-edit"
          @click="handleFieldList(scope.row.id)"
          style="margin: 0px 10px;width: 120px;"
        >字段管理</el-button>
        <el-button
          size="mini"
          type="success"
          icon="el-icon-edit"
          @click="handleUpdate(scope.row)"
          style="margin: 0px 10px;width: 60px;"
        >修改</el-button>
        <el-button
          size="mini"
          type="danger"
          icon="el-icon-delete"
          @click="handleDelete(scope.row)"
          style="margin: 0px 10px;width: 60px;"
        >删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 添加或修改公告对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="表单名称" prop="formName">
            <el-input v-model="form.formName" placeholder="请输入表单名" />
            <div id="textAreaWrap" v-show="false">
              <textarea id="textArea" v-model="form.test01" @input="test01"></textarea>
              <div v-show="testopen" id="placeholderDiv">这是一段<br>测试文本</div>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附加表名" prop="tableName">
            <el-input v-model="form.tableName" placeholder="系统会自动添加表前缀system_" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="表单性质" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio
                v-for="dict in dict.type.cms_form_type"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>

  <el-dialog :title="fieldTitle" :visible.sync="fieldOpen" width="980px" append-to-body>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddField"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="fieldLoading" :data="fieldList" @selection-change="handleSelectionChangeField">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="表单提示文字" align="center" prop="fieldText" width="100"/>
      <el-table-column label="数据字段名" align="center" prop="fieldName" width="100"/>
      <el-table-column label="数据类型" align="center" prop="dataType" width="100"/>
      <el-table-column label="默认值" align="center" prop="defaultValue" width="100"/>
      <el-table-column label="字段长度" align="center" prop="maxLength" width="100"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            icon="el-icon-edit"
            @click="handleUpdateField(scope.row)"
            style="margin: 0px 10px;width: 60px;"
          >修改</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDeleteField(scope.row)"
            style="margin: 0px 10px;width: 60px;"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>

  <!-- 添加或修改公告对话框 -->
  <el-dialog :title="fieldTitle" :visible.sync="fieldEditOpen" width="780px" append-to-body>
    <el-form ref="fieldForm" :model="fieldForm" :rules="fieldRules" label-width="140px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="表单提示文字" prop="fieldText">
            <el-input v-model="fieldForm.fieldText" placeholder="请输入表单提示文字" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="字段名称" prop="fieldName">
            <el-input v-model="fieldForm.fieldName" placeholder="字段名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据类型" prop="dataType">
            <el-radio-group v-model="fieldForm.dataType">
              <el-radio
                v-for="dict in dict.type.cms_field_datatype"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="默认值" prop="defaultValue">
            <el-input v-model="fieldForm.defaultValue" placeholder="默认值" />
            <br><span>定义select、radio、checkbox时，此处填写被选择的项目(用“,”分开，如“男,女,未知”)</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="字段长度" prop="maxLength">
            <el-input v-model="fieldForm.maxLength" placeholder="字段名称" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitFormField">确 定</el-button>
      <el-button @click="cancelField">取 消</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import {getFormListAll,getForm,saveForm,updateForm,deleteForm,getFieldList,getField,saveField,updateField,deleteField} from "@/api/cms/demo";

export default {
  name: "form",
  dicts: ['cms_form_type','cms_field_datatype'],
  data() {
    return {
      testopen:true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      fieldLoading: true,
      // 选中数组
      fieldIds: [],
      // 非单个禁用
      fieldSingle: true,
      // 非多个禁用
      fieldMultiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      formList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      fieldEditOpen: false,
      // 查询参数
      queryParams: {

      },
      // 表单参数
      form: {
        type: '1',
        test01: '',
      },
      // 表单校验
      rules: {
        formName: [
          { required: true, message: "表单名称不能为空", trigger: "blur" }
        ],
        tableName: [
          { required: true, message: "附加表不能为空", trigger: "change" }
        ]
      },
      fieldTitle: '字段管理',
      fieldOpen: false,
      fieldList: [],
      fieldForm: {
        formId: '',
        dataType: 'varchar',
      },
      fieldRules: {

      },
      selectFormId: '',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      getFormListAll().then(response => {
        this.formList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelField() {
      this.fieldEditOpen = false;
      this.resetField();
    },
    // 表单重置
    reset() {
      this.form = {
        fileName: undefined,
        tableName: undefined,
        type: '1',
      };
      this.resetForm("form");
    },
    // 表单重置
    resetField() {
      this.fieldForm = {
        fieldText: undefined,
        fieldName: undefined,
        dataType: 'varchar',
        defaultValue: undefined,
        maxLength: undefined,
      };
      this.resetForm("fieldForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    handleSelectionChangeField(selection) {
      this.fieldIds = selection.map(item => item.id)
      this.fieldSingle = selection.length!=1
      this.fieldMultiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加表单";
    },
    handleAddField() {
      this.fieldEditOpen = true;
      console.log(this.fieldForm.formId)
    },
    handleUpdateField(row){
      this.resetField();
      const id = row.id || this.ids
      getField(id).then(response => {
        this.fieldForm = response.data;
        this.fieldEditOpen = true;
      });
    },
    handleDeleteField(row){
      const fid = row.id || this.fieldIds
      this.$modal.confirm('是否确认删除编号为"' + fid + '"的数据项？').then(function() {
        return deleteField(fid);
      }).then(() => {
        getFieldList(this.fieldForm.formId).then(response=>{
          this.fieldList = response.data;
        })
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getForm(id).then(response => {
        this.form = response.data;
        this.form.type = ''+response.data.type;
        this.open = true;
        this.title = "修改表单";
      });
    },
    handleFieldList(formId){
      this.fieldForm.formId = formId;
      this.fieldLoading = true;
      getFieldList(formId).then(response=>{
        this.fieldList = response.data;
        this.fieldOpen = true;
        this.fieldLoading = false;
      })
      console.log(this.fieldForm.formId)
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateForm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            saveForm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitFormField(){
      this.$refs["fieldForm"].validate(valid => {
        if (valid) {
          if (this.fieldForm.id != undefined) {
            updateField(this.fieldForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.fieldEditOpen = false;
              getFieldList(this.fieldForm.formId).then(response=>{
                this.fieldList = response.data;
              })
            });
          } else {
            saveField(this.fieldForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.fieldEditOpen = false;
              getFieldList(this.fieldForm.formId).then(response=>{
                this.fieldList = response.data;
              })
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fid = row.id || this.ids
      this.$modal.confirm('是否确认删除公告编号为"' + fid + '"的数据项？').then(function() {
        return deleteForm(fid);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    test01:function (event){
      console.log(event,1111)
      if(this.form.test01==''){
        this.testopen = true
      }else{
        this.testopen = false
      }
    }
  }
}
</script>

<style scoped>
#textAreaWrap {
  position: relative;
  background-color: white;
}

#textArea {
  position: relative;
  z-index: 1;
  width: 350px;
  height: 100px;
  min-height: 100px;
  padding: 6px 12px;
  resize: vertical;
  background-color: transparent;
  border: 1px solid #a5a5a5;
}

#placeholderDiv {
  position: absolute;
  top: 0;
  padding-top: 4px;
  left: 16px;
  color: #a5a5a5;
  line-height: 20px;
}
</style>
